package com.ac.util;

import java.net.URLEncoder;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 */
public class StrUtils {

	public static String[] letter = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
			"q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

	/**
	 * 返回范围内的随机数
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int getRandom(int min, int max) {
		if (min > max) {
			min = min ^ max;
			max = min ^ max;
			min = min ^ max;
		}
		return new Random().nextInt(max - min + 1) + min;
	}

	/**
	 * 判断字符串是否不为空、空串、空格等不显示符
	 */
	public static boolean isNotEmpty(final String cs) {
		return !isEmpty(cs);
	}

	/**
	 * 判断字符串是否为空、空串、空格等不显示符
	 */
	public static boolean isEmpty(final String cs) {
		return cs == null || cs.length() == 0 || cs.trim().isEmpty() || cs.equals("null") || cs.equals("undefined");
	}

	/**
	 * 清除所有换行、空格、回车字符
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 对String进行UTF-8编码
	 * 
	 * @param pathVariable
	 * @return
	 */
	public static String strToUTF8(String str) {
		try {
			str = URLEncoder.encode(str, "utf-8");
		} catch (Exception e) {
		}
		return str;
	}

	public static String getRandomId() {
		return StrUtils.letter[StrUtils.getRandom(0, StrUtils.letter.length - 1)] + getRandom(100, 999);
	}

}
